package com.gcph.zhuoba.main.activitys;

import android.app.Activity;
import android.content.Intent;

import com.gcph.zhuoba.R;
import com.gcph.zhuoba.app.GcphApplication;
import com.gcph.zhuoba.util.SharePreferenceUtil;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	private Button signin_button;
	private Button app_tutor;
	private Button app_player;
	private EditText username_edit;
	private EditText password_edit;
	private SharePreferenceUtil spUtil;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        
        username_edit = (EditText) this.findViewById(R.id.username_edit);
        password_edit = (EditText) this.findViewById(R.id.password_edit);
		signin_button = (Button) this.findViewById(R.id.signin_button);
		
		app_tutor = (Button) this.findViewById(R.id.app_tutor);
		app_player = (Button) this.findViewById(R.id.app_player);
		
		spUtil = GcphApplication.getInstance().getSpUtil();
		app_tutor.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				spUtil.setUserType(getResources().getString(R.string.constant_tutor));
				Intent intent = new Intent(LoginActivity.this,MainActivity.class);
				startActivity(intent);
			}
		});
		
		app_player.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				spUtil.setUserType(getResources().getString(R.string.constant_player));
				Intent intent = new Intent(LoginActivity.this,MainActivity.class);
				startActivity(intent);
			}
		});
		
		//登陆 
		signin_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String loginName = username_edit.getText().toString();
				String password = password_edit.getText().toString();
				if(TextUtils.isEmpty(loginName))
				{
					Toast.makeText(LoginActivity.this, "请输入账号信息", Toast.LENGTH_SHORT).show();
					return;
				}
				
				if(TextUtils.isEmpty(password))
				{
					Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
					return;
				}
				
				//包含tutor即认为是老师
				if(loginName.contains(getResources().getString(R.string.constant_tutor)))
				{
					spUtil.setUserType(getResources().getString(R.string.constant_tutor));
				}else
				{
					spUtil.setUserType(getResources().getString(R.string.constant_player));
				}
				
				Intent intent = new Intent(LoginActivity.this,MainActivity.class);
				startActivity(intent);
			}
		});
    }
}